const cookieTool = {
    // 获取 Cookie
    getCookie: (name) => {
        const regex = new RegExp(`(?:(?:^|.*;\\s*)${encodeURIComponent(name)}\\s*=\\s*([^;]*).*$)|^.*$`);
        return decodeURIComponent(document.cookie.replace(regex, "$1")) || null;
    },

    // 设置 Cookie
    setCookie: (name, value, expireTime, options: any = {}) => {
        let cookieText = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;

        // 处理过期时间（支持秒数或 Date 对象）
        if (expireTime) {
            if (typeof expireTime === "number") {
                const date = new Date();
                date.setTime(date.getTime() + expireTime * 1000);
                cookieText += `; expires=${date.toUTCString()}`;
            } else if (expireTime instanceof Date) {
                cookieText += `; expires=${expireTime.toUTCString()}`;
            }
        }

        // 添加其他选项（path, domain, secure, sameSite）
        if (options.path) cookieText += `; path=${options.path}`;
        if (options.domain) cookieText += `; domain=${options.domain}`;
        if (options.secure) cookieText += "; secure";
        if (options.sameSite) cookieText += `; sameSite=${options.sameSite}`;

        document.cookie = cookieText;
    },

    // 删除 Cookie
    delCookie: (name, path, domain) => {
        cookieTool.setCookie(name, "", -1, { path, domain }); // 设置过期时间为过去
    }
};

export default cookieTool;